﻿using System.Collections.Generic;

namespace Com.ZX
{
    public class StateMachine
    {
        private Dictionary<int, IState> dictState = new Dictionary<int, IState>();
        public IState currentState { get; set; }

        public void InitState(int id)
        {
            if (!dictState.ContainsKey(id))
            {
                currentState = dictState[id];
            }
        }
        public IState GetState(int id)
        {
            if (dictState.ContainsKey(id))
                return dictState[id];
            return null; 
        }

        public void AddState(IState state)
        {
            if (!dictState.ContainsKey(state.Id))
            {
                dictState.Add(state.Id, state);
            }
        }

        public void RemoveState(int id)
        {
            if (dictState.ContainsKey(id))
            {
                dictState.Remove(id);
            }
        }

        public void RemoveAllState()
        {
            foreach (var item in dictState)
            {
                RemoveState(item.Key);
            }
        }
        public void ChangeState(int id)
        {
            if (dictState.ContainsKey(id))
            {
                currentState.ExitState();
                currentState = dictState[id];
                currentState.EnterState();
            }
        }

        public void Execute()
        {
            currentState.ExitState();
        }
    }
}
